<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>

    function binary(arr,target) {
        //[1,2,3,4,5,6]
        // 5 0
        let high = arr.length - 1;
        let low = 0;
        if(target < arr[low] || target > arr[high]){
            return false
        }
        let tmp_mid = -1;
        while(true){
            let mid = Math.ceil((high + low)/2);
            if(arr[mid] === target){
                return mid
            }else if(arr[mid] > target){
                high = mid -1
            }else { //mid<target
                low = mid +1
            }
            if(tmp_mid !== mid){
                tmp_mid = mid
                continue
            }else{
                return false
            }

        }
    }
    //[2,3,6,8]  4
    // 1. high 3 low 0 mid 2
    // 2. high 2 low 0 mid 1
    // 3. high 2 low 1 mid 2
    // 4. high 2 low 2 mid 2
    // 4. high 2 low 2 mid 2
    let arr = [2,3,5,7,11,13,17,19,23,29,31, 37, 41,43,47];
    let res = binary(arr,4);
    document.write(res)
</script>
</body>
</html>